package na.learn.java.threads;

public class TestLock implements Runnable {

	private String resource;

	private static boolean flag = false;

	public static void main(String[] args) {

		TestLock test = new TestLock();
		(new Thread(test)).start();
		(new Thread(test)).start();

	}

	public void run() {
		try {

			if (!flag) {
				flag = true;

				Thread.sleep(500);

				for (int counter = 0; counter < 100; counter++) {
					Thread.sleep(5);
					setResource("" + counter);
				}

			} else {
				for (int counter = 0; counter < 100; counter++){
					Thread.sleep(5);
					System.out.println(getResource());
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getResource() {
		System.out.println("GET at : " + System.currentTimeMillis());
		return resource;
	}

	public synchronized void setResource(String resource) {
		System.out.println("SET at : " + System.currentTimeMillis());
		this.resource = resource;
	}

}
